import { ElNotification, NotificationParams } from 'element-plus'

const notificationCommonOpt: NotificationParams = {
  customClass: 'el-notification--edge',
}

let keepOnly: AnyType
let keepOne: AnyType

export const notification = {
  send: (opt: NotificationParams) =>
    ElNotification({
      ...notificationCommonOpt,
      ...(opt as AnyType),
    }),
  success: (title: string, message?: string, duration = 2000) =>
    ElNotification({
      type: 'success',
      title,
      message,
      duration,
      ...notificationCommonOpt,
    }),
  error: (title: string, message?: string, duration = 2000) =>
    ElNotification({
      type: 'error',
      title,
      message,
      duration,
      ...notificationCommonOpt,
    }),
  warning: (title: string, message?: string, duration = 2000) =>
    ElNotification({
      type: 'warning',
      title,
      message,
      duration,
      ...notificationCommonOpt,
    }),
  info: (title: string, message?: string, duration = 2000) =>
    ElNotification({
      type: 'info',
      title,
      message,
      duration,
      ...notificationCommonOpt,
    }),
}

export const successNotify = (message: string) =>
  notification.send({ type: 'success', title: '操作成功', message })

export const waitingNotify = (message: string) =>
  notification.send({ type: 'info', message, duration: 0 })

export const warningNotify = (message: string) =>
  notification.send({ type: 'warning', title: '异常提示', message })

export const errorNotify = (message: string) =>
  notification.send({ type: 'error', title: '异常警报', message })

export const onlyNotify = (message: string) => {
  if (!keepOnly)
    keepOnly = ElNotification({
      message,
      duration: 2000,
      onClose: () => {
        keepOnly = undefined
      },
      customClass: 'el-notification--edge el-notification--error',
    })
}

export const onlyAlarm = () => {
  onlyNotify('故障信息已发送')
}

export const oneNotify = (message: string) => {
  if (keepOne) keepOne.close()
  setTimeout(() => {
    keepOne = ElNotification({
      message,
      duration: 2000,
      onClose: () => {
        keepOne = undefined
      },
      customClass: 'el-notification--edge el-notification--info',
    })
  })
}

export default notification
